package com.lj.ljminsu.controller;

import com.lj.ljminsu.util.ImageVerificationCode;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("yan")
public class YanController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 图片验证码
     *
     * @param response
     * @throws IOException
     */
    @CrossOrigin
    @RequestMapping("getVerifiCode")
    public void getVerifiCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /*
             1.生成验证码
             2.把验证码上的文本存在session中
             3.把验证码图片发送给客户端
             */
        ImageVerificationCode ivc = new ImageVerificationCode();     //用我们的验证码类，生成验证码类对象
        BufferedImage image = ivc.getImage();  //获取验证码
        String text = ivc.getText();//获取图形文本
        stringRedisTemplate.opsForValue().set("text",text,5L, TimeUnit.MINUTES);
        ivc.output(image, response.getOutputStream());//将验证码图片响应给客户端
    }
}

